package com.wufeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wufeng.dto.RichEvaluationDto;
import com.wufeng.dto.RichEvaluationQuestionDto;
import com.wufeng.entity.Evaluate;
import com.wufeng.entity.EvaluateRecord;
import com.wufeng.entity.QuestionOption;
import com.wufeng.service.EvaluateQuestionService;
import com.wufeng.service.EvaluateRecordService;
import com.wufeng.service.EvaluateService;
import com.wufeng.mapper.EvaluateMapper;
import com.wufeng.service.QuestionOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Hxh
 * @description 针对表【evaluate(心理测评表)】的数据库操作Service实现
 * @createDate 2024-01-14 23:39:19
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate>
        implements EvaluateService {

    @Autowired
    private EvaluateRecordService evaluateRecordService;

    @Autowired
    private EvaluateQuestionService evaluateQuestionService;

    @Autowired
    private QuestionOptionService questionOptionService;

    @Override
    public boolean addEvaCount(Integer evaluateId, Integer userId) {

        //判断是否有记录，有则返回
        long count = evaluateRecordService.count(new QueryWrapper<EvaluateRecord>().
                eq("evaluateId", evaluateId).
                eq("userId", userId));
        if (count > 0) {
            return true;
        }

        return this.baseMapper.addEvaCount(evaluateId);
    }

    /**
     * 保存并返回id
     *
     * @param evaluate
     * @return
     */
    @Override
    public Integer saveEvaluate(Evaluate evaluate) {
        this.baseMapper.insert(evaluate);
        Integer evaluateid = evaluate.getEvaluateid();
        return evaluateid;
    }


    @Transactional
    @Override
    public void saveRichEvaluate(RichEvaluationDto richEvaluationDto) {

        //设置创建时间
        richEvaluationDto.getEvaluate().setCreatedate(new Date());

        //1.保存测试返回测试id
        Integer evaluateId = saveEvaluate(richEvaluationDto.getEvaluate());
        //2.获取问题列表
        List<RichEvaluationQuestionDto> richEvaluationQuestionDtoList =
                richEvaluationDto.getRichEvaluationQuestionDtoList();
        //3.遍历问题列表
        for (RichEvaluationQuestionDto richEvaluationQuestionDto
                : richEvaluationQuestionDtoList) {
            //3.1每个问题设置关联的测试id
            richEvaluationQuestionDto.getEvaluateQuestion().setEvaluateid(evaluateId);

            //3.2插入问题返回问题id
            Integer questionId = evaluateQuestionService.
                    saveEvaluateQuestion(richEvaluationQuestionDto.getEvaluateQuestion());

            //3.3获取选项列表
            List<QuestionOption> questionOptionList =
                    richEvaluationQuestionDto.getQuestionOptionList();
            //3.4遍历选项列表
            for (QuestionOption questionOption : questionOptionList) {
                // 3.4.1.设置关联的问题id
                questionOption.setQuestionid(questionId);

                //3.4.2.插入选项
                questionOptionService.save(questionOption);

            }

        }
    }

    @Override
    public List<Evaluate> getEvaluationByCondition(String title, Integer type, Integer pageIndex, Integer pageSize) {
        Integer startIndex = (pageIndex-1) * pageSize;
        return this.baseMapper.getEvaluationByCondition(title,type,startIndex, pageSize);
    }

    @Override
    public Integer countEvaluationByCondition(String title, Integer type) {
        return this.baseMapper.countEvaluationByCondition(title,type);
    }
}




